#include "class.h"

#ifdef CLASS_DEBUGB
#define dbg_print(fmt, ...) printk(fmt, ##__VA_ARGS__)
#else
#define dbg_print(fmt, ...)
#endif

static struct kfifo gfifo[CLASS_CNT];
//class对象
static struct class *gfifo_cls;

//打开设备
static int gfifo_open(struct inode *inode, struct file *filep)
{
	uint32_t minor;

	//提取次设备号
	minor = MINOR(inode->i_rdev);
	if((minor-CLASS_MINOR) > (sizeof(gfifo)/sizeof(gfifo[0])))
		return EINVAL;
	//绑定对应的FIFO句柄
	filep->private_data = &gfifo[minor-CLASS_MINOR];
	return 0;
}

//释放设备，应用层执行最后一次close时执行
static int gfifo_release(struct inode *inode, struct file *filep)
{
	return 0;
}

//读设备
static ssize_t gfifo_read(struct file *filep, char __user *buf, size_t len, loff_t *pos)
{
	unsigned int copied;
	struct kfifo *fifo = (struct kfifo*)filep->private_data;

	if(kfifo_is_empty(fifo))
	{
		dbg_print("fifo is empty\r\n");
		return -EINVAL;
	}

	//将FIFO中的数据读取到应用层
	if(kfifo_to_user(fifo, buf, len, &copied) != 0)
	{
		dbg_print("Bad address\r\n");
		return -EFAULT;
	}
	return copied;
}

//写设备
static ssize_t gfifo_write(struct file *filep, const char __user *buf, size_t len, loff_t *pos)
{
	unsigned int copied;
	struct kfifo *fifo = (struct kfifo*)filep->private_data;

	if(kfifo_is_full(fifo))
	{
		dbg_print("fifo is full\r\n");
		return -EINVAL;
	}

	//将应用层的数据写入FIFO
	if(kfifo_from_user(fifo, buf, len, &copied) != 0)
	{
		dbg_print("Bad address\r\n");
		return -EFAULT;
	}
	return copied;
}

//设备号
static dev_t dev_id;
//cdev对象
static struct cdev gfifo_cdev;
//操作函数
static struct file_operations gfifo_ops = {
	.owner	  = THIS_MODULE,
	.write	  = gfifo_write,
	.read	   = gfifo_read,
	.open	   = gfifo_open,
	.release	= gfifo_release,
};

static int __init gfifo_init(void)
{
	int i;
	int err = 0;
	struct device *device;

	//动态初始化FIFO句柄
	for(i=0; i<CLASS_CNT; i++)
	{
		err = kfifo_alloc(&gfifo[i], 32, GFP_KERNEL);
		if(err != 0)
		{
			for(i=0; i<CLASS_CNT; i++)
				kfifo_free(&gfifo[i]);
			dbg_print("kfifo alloc failed\r\n");
			return err;
		}
	}

#ifndef USING_ALLOC_DEV_ID
	//合成设备号
	dev_id = MKDEV(CLASS_MAJOR, CLASS_MINOR);
	//静态注册字符设备号
	err = register_chrdev_region(dev_id, CLASS_CNT, CLASS_NAME);
	if(err != 0)
	{
		for(i=0; i<CLASS_CNT; i++)
			kfifo_free(&gfifo[i]);
		dbg_print("register chrdev failed\r\n");
		return err;
	}
#else
	//根据次设备号起始值动态注册字符设备号
	err = alloc_chrdev_region(&dev_id, CLASS_MINOR, CLASS_CNT, CLASS_NAME);
	if(err != 0)
	{
		for(i=0; i<CLASS_CNT; i++)
			kfifo_free(&gfifo[i]);
		dbg_print("alloc chrdev failed\r\n");
		return err;
	}
#endif
	//显示主设备号和次设备号
	for(i=0; i<CLASS_CNT; i++)
	{
		printk("gfifo %d ", i);
		printk("MAJOR %d ", MAJOR(dev_id + i));
		printk("MINOR %d ", MINOR(dev_id + i));
		printk("\r\n");
	}

	//初始化CDEV对象
	cdev_init(&gfifo_cdev, &gfifo_ops);
	gfifo_cdev.owner = THIS_MODULE;

	//向系统添加CDEV对象
	err = cdev_add(&gfifo_cdev, dev_id, CLASS_CNT);
	if(err != 0)
	{
		unregister_chrdev_region(dev_id, CLASS_CNT);
		for(i=0; i<CLASS_CNT; i++)
			kfifo_free(&gfifo[i]);
		dbg_print("add cdev failed\r\n");
		return err;
	}

	//创建class对象
	gfifo_cls = class_create(THIS_MODULE, CLASS_NAME);
	if(IS_ERR(gfifo_cls))
	{
		cdev_del(&gfifo_cdev);
		unregister_chrdev_region(dev_id, CLASS_CNT);
		for(i=0; i<CLASS_CNT; i++)
			kfifo_free(&gfifo[i]);
		dbg_print("class create failed");
		return PTR_ERR(gfifo_cls);
	}
	
	for(i = 0; i < CLASS_CNT; i++)
	{
		//在class创建设备，此时会产生热插拔事件
		device = device_create(gfifo_cls, NULL, dev_id+i, NULL, "gfifo%d", i);
		if(IS_ERR(device))
		{
			for(i = 0; i < CLASS_CNT; i++)
				device_destroy(gfifo_cls, dev_id+i);
			class_destroy(gfifo_cls);
			cdev_del(&gfifo_cdev);
			unregister_chrdev_region(dev_id, CLASS_CNT);
			for(i=0; i<CLASS_CNT; i++)
				kfifo_free(&gfifo[i]);
			dbg_print("device create failed");
			return PTR_ERR(device);
		}
	}
	return 0;
}

static  void __exit gfifo_exit(void)
{
	int i;
	
	//销毁class中的设备，此时会产生热插拔事件
	for(i = 0; i < CLASS_CNT; i++)
		device_destroy(gfifo_cls, dev_id+i);
	//销毁class对象
	class_destroy(gfifo_cls);
	//从内核删除CDEV对象
	cdev_del(&gfifo_cdev);
	//注销设备号
	unregister_chrdev_region(dev_id, CLASS_CNT);
	//释放FIFO
	for(i=0; i<CLASS_CNT; i++)
		kfifo_free(&gfifo[i]);
}

module_init(gfifo_init);
module_exit(gfifo_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("LF");
MODULE_DESCRIPTION("class cdev");
MODULE_ALIAS("class_cdev");